﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Core;


namespace GSLMS.WebAPP.Areas.RiskAudit.Controllers.AuditCon.AuditConSum
{
    public class AuditConSumController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "RiskAudit_AuditCon_AuditConSum"; }
        }

        [HttpGet]
        public ActionResult Index()
        {
            var searchParam = new TB_ADU_CONARR();
            searchParam.STATUS = 1;

            var model = serviceAduConArr.GetConSumList(searchParam);
            ViewData["RowCount"] = searchParam.RowCount;

            ViewData["SearchParma"] = searchParam;
            InitSelect();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            var searchParam = base.SearchPara<TB_ADU_CONARR>(form, new TB_ADU_CONARR());
            var model = serviceAduConArr.GetConSumList(searchParam);
            ViewData["RowCount"] = searchParam.RowCount;
            InitSelect();
            return View(model);
        }

        protected void InitSelect()
        {
            ViewData["systemID"] = EnumHelper.ToSelectList<GSLMS.Entity.Enum.ApplyEnum.RegSystemID>();
        }

        public ActionResult LoanTimeType()
        {
            return View();
        }


        public ActionResult Create(long id, int consumflag)
        {

            var model = serviceProInfo.GetByID(id);
            ViewData["ConSumFlag"] = consumflag;
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname");
            ViewData["AuditInfo"] = serviceAppAudit.GetRiskAuditConAduAuditInfo(id);
            ViewData["empName"] = CurentUser.EmpName;

            return View(model);
        }

        public ActionResult VConSum(long id)
        {

            var model = serviceProInfo.GetByID(id);
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname");
            ViewData["AuditInfo"] = serviceAppAudit.GetRiskAuditConAduAuditInfo(id);
            ViewData["empName"] = CurentUser.EmpName;
            ViewData["ConSumModel"] = serviceAduConSum.GetByProID(id);
            return View(model);
        }

        [HttpPost]
        public string CreateConSum(FormCollection form)
        {
            string result = string.Empty;
            var model = new TB_ADU_CONSUM();
            var proModel = new TB_APP_PROINFO();
            var AduConSchemeList = new List<TB_ADU_CONSCHEME>();
            long proID = 0;

            proID = Helper.ToLong(Request["id"]);
            model.PROID = proID;
            model.SUMBY = CurentUser.UserName;
            model.SUMDATE = DateTime.Now;
            model.ARRID = 0;
            model.ID = 0;
            model.ADURESULT = Helper.ToInt(Request["hidActiondo"]);
            model.SUMOPINION = Request["AuditContent"].Trim();
            model.REMARK = Request["remark"].Trim();
            model.TOUSER = Request["toUser"].Trim();
            //dwz_rowNum

            TryUpdateModel(proModel, form);

            //同意
            if (model.ADURESULT == 1 && (proModel.AduConSchemeList == null || proModel.AduConSchemeList.Count == 0))
            {
                result = string.Format(ajaxStr, 300, ResourceManager.NeedConScheme, navTabId, "", "");
            }
            else
            {
                model.AduConSchemeList = proModel.AduConSchemeList;
            }


            if (result== null||result==string.Empty)
            {
                try
                {
                    if (serviceAduConSum.Add(model))
                    { result = AjaxResultSucess; }
                    else
                    { result = AjaxResultFail; }
                }
                catch (Exception ex)
                {
                    base.ExceptionHand(ex);
                    result = AjaxResultFail;
                }
            }

            return "{" + result + "}";
        }

    }
}
